﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYCheckAllSprinklers : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new NotImplementedException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				List<Element> list = new List<Element>();
				int num = 0;
				ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Sprinklers);
				foreach (Element element in new FilteredElementCollector(document, document.ActiveView.Id).WherePasses(elementCategoryFilter).ToElements())
				{
					if (!this.IsSprinkerConnected(element))
					{
						num++;
						list.Add(element);
					}
				}
				if (num == 0)
				{
					YJKMessageBox.Information("视图中所有喷头都已连接");
				}
				else
				{
					YJKMessageBox.Information("视图中还有" + num + "个喷头没有连接");
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public bool IsSprinkerConnected(Element ele)
		{
			return YJKRevitTools.GetElementConnector(ele).IsConnected;
		}
	}
}
